package cs2340.g21.wms;

import java.util.ArrayList;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class NotePageActivity extends Activity {
	
	private static final String LOG_TAG = "debugger";
    private Note note;
    private ArrayList<Note> notes;
    private Coordinator coordinator;
    
    // Note attributes
    private String mName;
    private String mMsg;
    private Calendar mDate;
    
    // UI elements
    private ImageView mPhotoView;
    private TextView mNameView;
    private TextView mMsgView;
    private TextView mDateView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_page);
        note = (Note) getIntent().getExtras().get("note");
        notes = (ArrayList<Note>)getIntent().getExtras().get("notes");
        // Get details about the note
        mName = note.getOwner().getEmail();
        mMsg = note.getMessage();
        mDate = note.getDate();
        
        
        // Get the references to the UI components
        mNameView = (TextView) findViewById(R.id.note_page_name);
        mDateView = (TextView) findViewById(R.id.note_page_date);
        mMsgView = (TextView) findViewById(R.id.note_page_msg);
        
        setUIComponents();
    }
    
    private void setUIComponents() {
    	// Set the appropriate fields in the UI components
        if (mName != null) {
        	mNameView.setText(mName);
        }
        if (mMsg != null) {
        	mMsgView.setText(mMsg);
        }
        if (mDate != null) {
        	mDateView.setText(mDate.get(Calendar.DAY_OF_MONTH) + "/" + (mDate.get(Calendar.MONTH)+1));
        }
    }
    
    public void onBackPressed(View view) {
        Intent intent = new Intent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.putExtra("notes", notes);
        intent.setClass(NotePageActivity.this, NoteListActivity.class);
        startActivity(intent);
        finish();
    }
    
    public void reply(View view){
        AlertDialog.Builder notifyBuild = new AlertDialog.Builder(this);
        notifyBuild.setTitle("Contact user");
        final EditText input = new EditText(this);
        //String email = getIntent().getStringExtra(EXTRA_EMAIL);
        //input.setText(email);
        notifyBuild.setView(input);
        
        notifyBuild.setPositiveButton(R.string.prompt_confirm, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String message = input.getText().toString();
                Note n = new Note(message, note.getOwner(), coordinator.getUser(), Calendar.getInstance());
                coordinator.getAccount(note.getOwner().getId()).addNote(n);
            }
        });
        
        notifyBuild.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                
            }
            
        });
        
        notifyBuild.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_item_page, menu);
        return true;
    }

}
